package com.allen.service;

import com.allen.bean.Attention;
import com.allen.bean.Result;
import com.allen.mapper.AttentionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.DataTruncation;
import java.util.List;

/**
 * @Author: allen
 * @Data: 2021.06.27.14:42
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class AttentionService {

    @Autowired
    private AttentionMapper attentionMapper;    //注入mapper依赖

    /**
     * 查出所有关注的人
     * @param attention
     * @return
     */
   /* public List<Attention> selu(Attention attention){
        List list = attentionMapper.findUsernameByUsername(attention.getUsername());
        attentionMapper.findUsernameByUsername(attention.getUsername());
        return list;
    }*/
    public Result selu(Attention attention){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);
        List<Attention> list=attentionMapper.findUsernameByUsername(attention.getUsername());
        if(list.size()==0){
            result.setMsg("没有查询到关注的人");
            result.setSuccess(false);
        }else{
            result.setMsg("查询成功,存在关注的人");
            result.setSuccess(true);
            result.setDetail(list);
        }
        return result;
    }

    /**
     * 增加关注
     * @param attention
     * @return
     */
    public Result add(Attention attention){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);

        Attention exitId = attentionMapper.findIdByName(attention.getUsername(),attention.getA_username());
        /*Integer exitName = attentionMapper.addAttention(attention);*/
        if (exitId==null){//没有关注
            attentionMapper.addAttention(attention);
            result.setMsg("关注成功");
            result.setSuccess(true);
        }else{
            result.setMsg("已经关注");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 取消关注
     * @param attention
     * @return
     */
    public Result del(Attention attention){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);

        Attention exitId = attentionMapper.findIdByName(attention.getUsername(),attention.getA_username());

        if(exitId!=null){//已经关注
            attentionMapper.delectName(attention);
            result.setMsg("取关成功");
            result.setSuccess(true);
        }else {
            result.setMsg("未关注");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 查询是否关注某人
     */
    public Result find(Attention attention){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);
        Attention flag=attentionMapper.findIdByName(attention.getUsername(),attention.getA_username());
        if(flag!=null){
            result.setMsg("取消关注");
            result.setSuccess(true);
        }else{
            result.setMsg("添加关注");
            result.setSuccess(false);
        }
        return  result;
    }
}
